package com.fingard.dsp.bank.directbank.jxb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 嘉兴银行余额查询
 * Created by F on 2019/08/07
 */
public class JXB01Balance extends JXB01Base implements IBalance {

    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{

            RetAtsBalDetail detail = new RetAtsBalDetail();
            //======拼接报文======
            JXB01Header header = new JXB01Header();
            header.trsCode="B2EActBalQry";
            XmlTextWriter xmlReq = new XmlTextWriter();
            writeReqHeader(xmlReq,header);
            xmlReq.writeEndElement();
            xmlReq.writeStartElement("Body");
            xmlReq.writeStartElement("List");
            while (p_req7506.readRecord()) {
                xmlReq.writeStartElement("Map");
                xmlReq.writeElementString("AcNo",p_req7506.readedDetail.actNum);//账号
                xmlReq.writeEndElement();
            }
            xmlReq.writeEndDocument();
            //======拼接报文======
            //发送请求报文
            String[] bankResult = sendToBank(xmlReq.toXmlString(),header.trsCode);
            //处理响应报文
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSb = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                if (tmpBkReader != null) {
                    String respCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    if ("000000".equals(respCode)) {//交易成功
                        while(tmpBkReader.read("/Message/Body/List/Map")) {
                            if("000000".equals(tmpBkReader.readedXElement.getSingleTextNotNull("ReturnCode"))){
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnMsg");
                                detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("Currency"));
                                detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("AcNo");
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBal");
                                detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Balance");
                            }else {
                                detail.respCode = "-1";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnMsg");
                            }
                            retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = respInfo;
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpSb.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
